{% extends 'web/layout/manage.html' %}
{% load static %}
{% block title %}Wiki{% endblock title %}
{% block css %}
    <link rel="stylesheet" href="{% static 'plugins/editor-md/css/editormd.min.css' %}">
    <link rel="stylesheet" href="{% static 'plugins/editor-md/css/editormd.preview.min.css' %}">
<style>

    .panel-default{
        margin-top:10px;
    }
    .panel-default .panel-heading {
            display: flex;
            flex-direction: row;
            justify-content: space-between;
        }
    .panel-body{
        padding:0px;
    }
    .title_list{
        border-right:1px solid #dddddd;
        min-height: 500px;
    }
    .content{
        border-left:1px solid #dddddd;
        min-height: 600px;
        margin-left: -1px;
    }
    .editormd-fullscreen{
            z-index: 1001;
        }

</style>
{% endblock css %}
{% block content %}

<!-- panel -->
<div class="container-fluid">    <!-- container-fluid -->
    <div class="panel panel-default">
        <div class="panel-heading">
            <div>
                <i class="fa fa-book" aria-hidden="true"></i> wiki文档
            </div>        
            <div>
                {% if wiki_editable %}
                    <a type="button" class="btn btn-warning btn-xs" href={% url 'web:wiki_edit' project_id=request.tracer.project.id wiki_id=wiki_obj.id %}>编辑</a>
                    <a type="button" class="btn btn-danger btn-xs" href={% url 'web:wiki_delete' project_id=request.tracer.project.id wiki_id=wiki_obj.id %}>删除</a>
                {% endif %}
                <a type="button" class="btn btn-success btn-xs" href={% url 'web:wiki_add' project_id=request.tracer.project.id %}>新建</a>
            </div>
            
        </div>
        <div class="panel-body">
            <div class="col-sm-3 title_list">
                <ul id="catalog">
                </ul>
            </div>
            <div class="col-sm-9 content">
            {% if wiki_obj %}
               <!--  <H2><<{{wiki_obj.title}}>></h2> -->
                <div id="id_previewMarkdown">
                    <textarea>{{wiki_obj.content}}</textarea>
                </div>
            {% elif forms %}
                <form method="POST" novalidate>
                    {% csrf_token %}
                    <div>
                        {% for field in forms %}
                            {% if field.name == "content" %}
                                <div>
                                    <label for="{{field.id_for_label}}">{{field.label}}</label>
                                </div>
                                <div id="id_editor">
                                    {{field}}
                                </div>
                                <span class="error_msg">{{field.errors.0}}</span>
                            {% else %}

                                <div>
                                    <label for="{{field.id_for_label}}">{{field.label}}</label>
                                </div>
                                <div> 
                                    {{field}}
                                </div>
                                <span class="error_msg">{{field.errors.0}}</span>
                            {% endif %}
                        {% endfor %}
                        {% if forms.initial %}
                            <button type="submit" class="btn btn-primary">修  改</button>
                        {% else %}
                            <button type="submit" class="btn btn-primary">提  交</button>
                        {% endif %}
                    </div>
                </form>


            {% else %}
                <div id="id_previewMarkdown">
                <textarea>《{% if request.tracer.project %}{{request.tracer.project.name}}{% endif %}》wiki文档</textarea>
                </div>
                <a href={% url 'web:wiki_add' project_id=request.tracer.project.id %}>
                    <i class="fa fa-plus-circle" aria-hidden="true"></i>新建文档
                </a>
            {% endif %}
            </div>
        </div>
    </div>
<div>
{% endblock content %}
{% block js %}
    <script src="{% static 'plugins/editor-md/editormd.min.js' %}"></script>
    
    <script src="{% static 'plugins/editor-md/lib/marked.min.js' %}"></script>
    <script src="{% static 'plugins/editor-md/lib/prettify.min.js' %}"></script>
    <script src="{% static 'plugins/editor-md/lib/raphael.min.js' %}"></script>
    <script src="{% static 'plugins/editor-md/lib/underscore.min.js' %}"></script>
    <script src="{% static 'plugins/editor-md/lib/sequence-diagram.min.js' %}"></script>
    <script src="{% static 'plugins/editor-md/lib/flowchart.min.js' %}"></script>
    <script src="{% static 'plugins/editor-md/lib/jquery.flowchart.min.js' %}"></script>




<script>
    $(function(){
        initCatalog();
        initMarkDown();
        initPreivewMarkdown();
    });

    function initMarkDown(){

         if ($('#id_editor').length > 0) {
            var editor = editormd("id_editor", {
                height:500,
                //height:"100%",
                placeholder:"请输入内容",
                path:"{% static 'plugins/editor-md/lib/' %}",
                imageUpload:true,				//使能图片上传
                imageFormats:["jpg",'jpeg','png','gif'],		//图片格式
                imageUploadURL: "{% url 'web:wiki_upload' project_id=request.tracer.project.id %}" //图片上传地址

            });
         }
    }

    function initPreivewMarkdown() {
        var preview = editormd.markdownToHTML("id_previewMarkdown", {
                htmlDecode: "style,script,iframe"		//过滤脚本,iframe防止xss攻击
        });
    }


    function initCatalog(){
        //var nm1=$('input[name="inputname"]').val();
        //var var_nm1 = $.cookie('csrftoken');
        //var id_user = $('#id_user').val();
        $.ajax({
                url: "{% url 'web:wiki_catalog' project_id=request.tracer.project.id %}",
                type: 'GET',
                //headers:{'X-CSRFToken': token},
                //data: { "id_user":id_user},
                dataType:'JSON',
                success:function(arg){
                    if (arg.status){
                        //console.log(arg.objs);
                        var WIKI_URL = "{% url 'web:wiki' project_id=request.tracer.project.id %}"
                        $.each(arg.objs,function(index,value){//对arg.msg进行循环
                            //console.log(index,value);
                            var li = $("<li>").attr('id','id_'+value.id).append(($("<a>").attr('href',WIKI_URL+"?wiki_id="+value.id)).text(value.title)).append($("<ul>"));
                            if (!value.parent){
                                $('#catalog').append(li);
                            }
                            else{
                                $('#id_'+value.parent).children('ul').append(li);
                            }
                            //console.log(value.id,value.title,value.parent)
                        })
                    }
                    else{
                        alert('格式不识别');
                    };
                }
            })
    }
</script>

{% endblock js %}